package com.shqcjd.preinstallmobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.api.ApiService;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.preinstallmobile.model.ResponseData;
import com.shqcjd.preinstallmobile.model.pojo.InspectionTaskBean;
import com.shqcjd.preinstallmobile.ui.adapter.InspectionEquimpentAdapter;
import com.shqcjd.preinstallmobile.ui.base.BaseActivity;
import com.shqcjd.preinstallmobile.ui.base.BasePresenter;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 已巡检-设备列表
 */
public class InspectionEquimpentListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private InspectionEquimpentAdapter equimpentAdapter;
    private int taskId;
    private View notDataView;
    private Context mContext;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_inspection_equimpent_list;
    }

    @Override
    public void initView() {
        mContext = this;
        tvToolbarCenter.setText("设备列表");
        taskId = getIntent().getIntExtra("taskId", -1);
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvList.getParent(), false);
        initAdapter();


    }

    @Override
    public void initData() {
        refresh();
    }


    private void initAdapter() {
        swipeRefresh.setOnRefreshListener(this);
        equimpentAdapter = new InspectionEquimpentAdapter();
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.setAdapter(equimpentAdapter);
        equimpentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                InspectionTaskBean.ListBean item = (InspectionTaskBean.ListBean) adapter.getItem(position);
                Intent intent = new Intent(mContext, EquipmentInspectionRecordActivity.class);
                intent.putExtra("tId", taskId);
                intent.putExtra("kks", item.kks);
                startActivity(intent);
            }
        });
    }

    private void refresh() {
        ApiService.getCheckList(taskId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<InspectionTaskBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showRefreshView(true);
                    }

                    @Override
                    public void _onNext(ResponseData<InspectionTaskBean> retData) {
                        if (retData.retCode == 10000) {
                            int size = retData.data.list.size();
                            if (size > 0) {
                                equimpentAdapter.setNewData(retData.data.list);
                            } else {
                                equimpentAdapter.setNewData(null);
                                equimpentAdapter.setEmptyView(notDataView);
                            }
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        showRefreshView(false);
                    }

                    @Override
                    public void _onComplete() {
                        super._onComplete();
                        showRefreshView(false);
                    }
                });
    }
    public void showRefreshView(Boolean refresh) {
        swipeRefresh.post(new Runnable() {
            @Override
            public void run() {
                swipeRefresh.setRefreshing(refresh);
            }
        });
    }

    @OnClick(R.id.iv_toolbar_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        refresh();
    }
}
